﻿using Pay.DTO;
using Pay.IService;
using Pay.Service;
using Pay.TO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Pay.Distribution.Controllers {
    public class DistributionController : GeneralController {

        private IProductService _iProductService;
        private IMerchantService _imerchantService;
        private ICategoryService _iCategoryService;
        private IDistributionProductService _idistributionProductService;

        public DistributionController(IProductService iProductService, IMerchantService merchantService, ICategoryService categoryService, IDistributionProductService distributionProductService) {
            _iProductService = iProductService;
            _imerchantService = merchantService;
            _iCategoryService = categoryService;
            _idistributionProductService = distributionProductService;


        }
        /// <summary>
        /// oem分销供应商产品
        /// </summary>
        /// <param name="categoryid"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult Save(long categoryid, long id) {
            ApiResultMessage api = new ApiResultMessage();
            var model = new distribution_product_dto();
            model.categoryid = categoryid;
            model.merchantid = currentUser.user_id;
            model.productid = id;
            if (_idistributionProductService.Add(model) > 0) {
                api.result_code = 1000;
                api.result_msg = "操作成功";
            } else {
                api.result_msg = "系统异常";
                api.result_code = -1;
            }
            return Json(ResultToJson.Json(api), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 获取oem商下面的产品
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public JsonResult Gets(GoodsQuery query) {

            var list = _idistributionProductService.Get(currentUser.user_id);
            var ids = list.Select(c => c.productid).ToArray(); //String.Join(",", list.Select(x=>x.productid));
            var merchant = _imerchantService.Get(currentUser.user_id);
            if (merchant.usertype == UserType.distributor)
                query.merchant_id = merchant.oem_id.Value;
            if (ids != null && ids.Length > 0) {
                query.Ids = ids;
            }
            var model = _iProductService.Get(query);
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 获取供应商
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public JsonResult GetsMerchant(MerchantQuery query) {
            // query.certify = 2;
            // query.auth_status = AuthStatusEnum.Auth;
            query.is_supplier = true;
            query.parent_id = currentUser.user_id;

            var model = _imerchantService.GetMerchant(query);
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }
        public JsonResult Detail(long id) {
            var model = _imerchantService.Get(id);
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// oem分销供应商
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        //public JsonResult Post(merchant_dto model) {
        //    ApiResultMessage api = new ApiResultMessage();
        //    var models = _imerchantService.Get(model.id);
        //    if (models != null) {
        //        //    model.ToDtoObject<merchant_dto>(models);
        //        models.parent_id = currentUser.user_id;
        //        models.rate = model.rate;
        //        models.poundage = model.poundage;
        //    }
        //    if (_imerchantService.UpdateParent(models) > 0) {
        //        api.result_code = 1000;
        //        api.result_msg = "操作成功";
        //    } else {
        //        api.result_msg = "系统异常";
        //        api.result_code = -1;
        //    }
        //    return Json(ResultToJson.Json(api), JsonRequestBehavior.AllowGet);
        //}


        /// <summary>
        /// 查询子类、将子类放在children中
        /// </summary>
        /// <returns></returns>
        public JsonResult Categorys() {
            var model = _iCategoryService.GetAll(currentUser.user_id);
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 获取当前分销商的分销商品
        /// </summary>
        /// <returns></returns>
        public JsonResult GetProducts() {
            var model = _idistributionProductService.Get(currentUser.user_id).ToDtoList<distribution_product_ex>();
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 下架当前分销商商品
        /// </summary>
        /// <returns></returns>
        public JsonResult SoldOut(long id) {
            var result = _idistributionProductService.SoldOut(id);
            return Json(ResultToJson.Json(result >= 0 ? "下架成功" : "下架失败", result), JsonRequestBehavior.AllowGet);
        }
    }
}